package com.yaorange.view;

import com.yaorange.controller.CustomerController;
import com.yaorange.pojo.Customer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class TestView {
    private static CustomerController customerController=new CustomerController();
    //增加客户
    public static void addCustomer() throws ParseException {
        System.out.println("请输入客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        System.out.println("请输入客户名称:");
        String name=new Scanner(System.in).nextLine();
        System.out.println("请输入客户生日:(yyyy年MM月dd日)");
        String day=new Scanner(System.in).nextLine();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date birthday=df.parse(day);
        System.out.println("请输入客户性别:(男/女)");
        char gender=new Scanner(System.in).next().charAt(0);
        Calendar calendar = Calendar.getInstance();
        Date calendarTime = calendar.getTime();
        Customer customer = new Customer(id, name, birthday, gender, calendarTime);
        customerController.addCustomer(customer);
        customerController.commit();
    }
    //删除客户
    public static void deleteCustomerById(){
        System.out.println("请输入你要删除的客户的客户编号:");
        String id=new Scanner(System.in).next();
        customerController.deleteCustomerById(id);
        customerController.commit();
    }
    //批量删除
    public static void batchDelete(){
        System.out.println("请输入你要批量删除的客户的客户编号，以/连接(例1001/1002/1003):");
        String ids=new Scanner(System.in).next();
        customerController.batchDelete(ids);
        customerController.commit();
    }
    //修改客户
    public static void updateCustomerById() throws ParseException {
        System.out.println("请输入需要修改的客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        System.out.println("请输入新的客户名称:");
        String name=new Scanner(System.in).nextLine();
        System.out.println("请输入新的客户生日:(yyyy年MM月dd日)");
        String day=new Scanner(System.in).nextLine();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date birthday=df.parse(day);
        System.out.println("请输入新的客户性别:(男/女)");
        char gender=new Scanner(System.in).next().charAt(0);
        customerController.updateCustomerById(id,name,birthday,gender);
        customerController.commit();
    }
    //查找客户
    public static void queryCustomerById(){
        System.out.println("请输入需要查找的客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        customerController.queryCustomerById(id);
    }

    public static void main(String[] args) throws ParseException {
        System.out.println("======================客户管理系统========================");
        System.out.println("========================显示=============================");
        customerController.showCustomers();

        while (true) {
            System.out.println("=======================系统操作==========================");
            System.out.println("=====================输入1：增加客户======================");
            System.out.println("=====================输入2：删除客户======================");
            System.out.println("=====================输入3：批量删除======================");
            System.out.println("=====================输入4：修改客户======================");
            System.out.println("=====================输入5：查找客户======================");
            System.out.println("=====================输入6：显示信息======================");
            System.out.println("=====================输入0：退出系统======================");
            System.out.print("请输入操作的代号:");
            Scanner scanner = new Scanner(System.in);
            int n=scanner.nextInt();
            switch (n) {
                case 1:
                    addCustomer();
                    break;
                case 2:
                    deleteCustomerById();
                    break;
                case 3:
                    batchDelete();
                    break;
                case 4:
                    updateCustomerById();
                    break;
                case 5:
                    queryCustomerById();
                    break;
                case 6:
                    customerController.showCustomers();
                    break;
                case 0:
                    customerController.over();
                    return;
            }
        }

    }
}
